package com.fxkj.cloud.drms.domain.ts.repository;

import com.fxkj.cloud.dbs.common.constants.DataType;
import com.fxkj.cloud.dbs.common.model.DeviceAttributeKey;
import com.fxkj.cloud.dbs.common.model.aggregation.IntervalType;
import com.fxkj.cloud.dbs.common.model.aggregation.TsQuery;
import com.fxkj.cloud.dbs.common.model.aggregation.TsQueryResult;
import com.fxkj.cloud.dbs.common.model.kv.KvEntry;
import com.fxkj.cloud.dbs.common.model.kv.TsKvEntry;
import com.fxkj.cloud.drms.domain.device.repository.*;
import com.google.common.util.concurrent.ListenableFuture;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * <p>description: 时序数据存储</p>
 * @author ww
 * @date 2025/3/18 10:03
 */
public interface TimeseriesRepository {

    ListenableFuture<Void> createSuperTable(UUID rootOrganizationId, UUID productId, Map<String, DataType> attributeKeyDataTypeMap);

    ListenableFuture<Void> addProductAttribute(UUID productId, Map<String, DataType> attributeKeyDataTypeMap);

    ListenableFuture<Void> removeProductAttribute(UUID productId, Set<String> attributeKeys);

    void createSonTable(UUID rootOrganizationId, UUID productId, UUID deviceId);

    ListenableFuture<Integer> save(UUID rootOrganizationId, UUID productId, UUID deviceId, List<TsKvEntry> ts);

    ListenableFuture<List<TsKvEntry>> getLatestTsKv(UUID deviceId, List<DeviceAttributeKey> deviceAttributeKeys);

    ListenableFuture<List<TsKvEntry>> getHistoryTsKv(UUID deviceId, List<DeviceAttributeKey> deviceAttributeKeys, long startTime, long endTime, Integer limit);

    ListenableFuture<List<TsKvEntry>> queryLastValueForString(UUID deviceId, List<String> attributeKeys);

    /**
     * 获取设备指定key的最新属性
     *
     * @param productIds
     * @param attributeKeys
     * @param organizationId
     * @return
     */
    ListenableFuture<Map<UUID, Map<String, KvEntry>>> queryDevicesLastValueByProductId(Set<UUID> productIds, List<String> attributeKeys, UUID organizationId);

    ListenableFuture<Void> saveDeviceStatus(DeviceStatus status);

    /**
     * 获取产品状态统计
     *
     * @param organizationId
     * @param productIds
     * @param selectTime
     * @return
     */
    ListenableFuture<ProductStatusStatistics> getProductStatusStatistics(UUID organizationId, Set<UUID> productIds, Long selectTime);

    /**
     * 获取产品下设备状态变更历史
     *
     * @param organizationId
     * @param productIdsByModelId
     * @param productId
     * @param deviceId
     * @param startTime
     * @param endTime
     * @param intervalValue
     * @param intervalTypeEnum
     * @return
     */
    ListenableFuture<ProductDeviceStatusChangeHistory> getProductDeviceStatusChangeHistory(UUID organizationId, Set<UUID> productIdsByModelId, UUID productId, UUID deviceId, Long startTime, Long endTime, Integer intervalValue, IntervalType intervalTypeEnum);

    /**
     * 获取产品下设备亮度统计
     * @param organizationId
     * @param productIds
     * @param startTime
     * @param endTime
     * @return
     */
    ListenableFuture<List<ProductDeviceLightStatistics>> getProductLightStatistics(UUID organizationId, Set<UUID> productIds, Long startTime, Long endTime);

    /**
     * 获取设备最新状态
     * @return
     */
    ListenableFuture<List<DeviceStatusLatestInfo>> getDeviceLatestStatusInfo();

    /**
     * 聚合查询设备历史数据
     * @param query
     * @return
     */
    ListenableFuture<List<TsQueryResult>> tsQuery(TsQuery query);

}
